package com.example.android_web_4;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.entity.TbStu;
import com.example.entity.TbUsers;
import com.example.utils.BitMapUtils;
import com.example.utils.CallBack;
import com.example.utils.FileUtils;
import com.example.utils.HttpUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StuActivity extends Activity {

    private ListView stuListView;
    private List<Map<String,Object>> stuList;
    private MyBaseAdapter adapter;
    String basePath=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stu);
        stuListView=(ListView)findViewById(R.id.stuList);
        basePath="http://"+getResources().getString(R.string.ipaddress)+":"+getResources().getString(R.string.serverport)+"/" +
                getResources().getString(R.string.project);
    }

    @Override
    protected void onStart() {
        super.onStart();
        loadData();//查询出所有学生信息并封装到集合中
    }

    //调用Web服务，查询所有学生信息
    public void loadData(){
        String url=basePath+"/TbStuServlet?method=stuList";
        HttpUtils.doGet(url, new CallBack() {
            @Override
            public void success(String str) {
                stuList=new ArrayList<Map<String, Object>>();
                List<TbStu> list=new Gson().fromJson(str,new TypeToken<List<TbStu>>(){}.getType());
                for(TbStu stu:list){
                    Map map=new HashMap();
                    map.put("_id",stu.get_id());
                    map.put("sname",stu.getSname());
                    map.put("sage",stu.getSage());
                    map.put("ssex",stu.getSsex());
                    map.put("shobby",stu.getShobby());
                    map.put("simage",stu.getSimage());
                    map.put("sintime",stu.getSintime());
                    map.put("cname",stu.getCls().getCname());
                    stuList.add(map);
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter=new MyBaseAdapter(StuActivity.this,stuList);
                        stuListView.setAdapter(adapter);
                    }
                });
            }
            @Override
            public void failed(String str) {
            }
        });

    }

    private class MyBaseAdapter extends BaseAdapter{

        private List<Map<String,Object>> list;
        private Context context;
        public MyBaseAdapter(Context context,List<Map<String,Object>> list){
            this.context=context;
            this.list=list;
        }
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            final ListItem listItem;
            if(view==null){
                listItem=new ListItem();
                view=View.inflate(context,R.layout.layout_stulist,null);
                listItem.checkItem=(CheckBox)view.findViewById(R.id.itemId);
                listItem.stuImage=(ImageView)view.findViewById(R.id.stuImage);
                listItem.sname=(TextView)view.findViewById(R.id.sname);
                listItem.cname=(TextView)view.findViewById(R.id.cname);
                listItem.sage=(TextView)view.findViewById(R.id.sage);
                listItem.ssex=(TextView)view.findViewById(R.id.ssex);
                listItem.shobby=(TextView)view.findViewById(R.id.shobby);
                view.setTag(listItem);
            }else{
                listItem=(ListItem) view.getTag();
            }
            Map item=list.get(i);//取出一项的值
            listItem.checkItem.setTag(item.get("_id").toString());
            listItem.cname.setText("班级:"+item.get("cname"));
            listItem.sname.setText("学生姓名:"+item.get("sname"));
            listItem.sage.setText("年龄:"+item.get("sage"));
            listItem.ssex.setText("性别:"+item.get("ssex"));
            listItem.shobby.setText("爱好:"+item.get("shobby"));
            String imagePath=item.get("simage").toString();
            final String path=getApplicationContext().getFilesDir().getAbsolutePath()+imagePath;
            if(FileUtils.existsFile(path)){//判断图像文件是否存在
                Bitmap bm= BitMapUtils.getSmallBitmap(path);
                listItem.stuImage.setImageBitmap(bm);
            }else{
                String fromPath=basePath+"/"+imagePath;
                FileUtils.downLoadFile(fromPath, path, new CallBack() {
                    @Override
                    public void success(String str) {
                        Bitmap bm= BitMapUtils.getSmallBitmap(path);
                        listItem.stuImage.setImageBitmap(bm);
                        adapter.notifyDataSetChanged();
                    }
                    @Override
                    public void failed(String str) {
                    }
                });
            }
            //
            return view;
        }
    }

    //定义一个Item所有拥有的控件
    private class ListItem{
        public CheckBox checkItem;
        public ImageView stuImage;
        public TextView sname;
        public TextView sage;
        public TextView ssex;
        public TextView shobby;
        public TextView cname;
    }

    //跳转到添加学生窗体
    public void toAddStu(View view){
        Intent intent=new Intent(this,AddStuActivity.class);
        startActivity(intent);
    }
}
